<?php

use home\assets\PageAsset;
use home\models\Area;
use libs\Utils;
use yii\bootstrap\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $data \home\models\Cart[] */

$this->title = '待确认';
$this->params['breadcrumbs'][] = $this->title;
$this->params['backUrl'] = Url::to(['site/index']);

PageAsset::register($this)->init([
    'css' => [
        'css/cart.css',
    ],
]);
?>

    <div class="page-body">
        <div class="cart-list clearfix">
            <div class="container">
                <?php $form = ActiveForm::begin([
                    'action' => Url::to(['order']),
                    'method' => 'post',
                ]); ?>
                <h3 style="margin-bottom: 20px;">填写项目信息</h3>

                <table class="table" style="margin-bottom: 30px;">
                    <tr>
                        <td width="10%" class="text-right">姓名</td>
                        <td width="5%">&nbsp;</td>
                        <td><input type="text" class="form-control form-control-inline" name="name" /></td>
                    </tr>
                    <tr>
                        <td class="text-right">电话</td>
                        <td>&nbsp;</td>
                        <td><?= Utils::starcode(Yii::$app->user->identity->mobile) ?></td>
                    </tr>
                    <tr>
                        <td class="text-right">所在城市</td>
                        <td>&nbsp;</td>
                        <td>
                            <?= Html::dropDownList('area_id', '', Area::getSelectData(), [
                                'class' => 'form-control form-control-inline',
                                'ajax-select' => Url::to(['ajax/select', 'model' => Area::className(), 'input' => 'area_id']),
                            ]) ?>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">详细地址</td>
                        <td>&nbsp;</td>
                        <td>
                            <div class="row">
                                <div class="col-lg-6">
                                    <input type="text" class="form-control" name="address" />
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="text-right">订单备注</td>
                        <td>&nbsp;</td>
                        <td>
                            <div class="row">
                                <div class="col-lg-6">
                                    <input type="text" class="form-control" name="memo" />
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>

                <h3 style="margin-bottom: 20px;">订单信息</h3>

                <table class="table table-striped">
                    <tr>
                        <th width="15%">商品</th>
                        <th width="15%">款式</th>
                        <th width="60%">规格</th>
                        <th width="10%">数量</th>
                    </tr>
                    <?php $total = 0; ?>
                    <?php if($data){ ?>
                        <?php foreach($data as $datum){ ?>
                            <tr>
                                <td>
                                    <img src="<?= $datum->product->getPreviewUrl(100, 100) ?>" />
                                    <input type="hidden" name="cart_ids[]" value="<?= $datum->id ?>" />
                                </td>
                                <td class="cart-product-name"><?= $datum->product->name . $datum->product->category->name ?></td>
                                <td class="cart-product-attr"><?= $datum->showAttrs() ?></td>
                                <td class="cart-product-quantity"><?= $datum->quantity ?></td>
                            </tr>
                            <?php $total += $datum->quantity; ?>
                        <?php } ?>
                    <?php }else{ ?>
                        <td colspan="5" class="text-center" style="padding: 30px">没有商品！</td>
                    <?php } ?>
                    <tfoot>
                    <tr>
                        <td class="text-right"> &nbsp; </td>
                        <td></td>
                        <td class="text-right">商品数量</td>
                        <td>
                             <span class="quantity-total"><?= $total ?></span> 件 &nbsp; &nbsp;
                        </td>
                    </tr>
                </tfoot>
                </table>

                <div style="padding: 30px; background: #f3f3f3; text-align: right; font-size: 16px; color: #d30000;">您提交项目订单以后24小时内，我们会安排1对1的专属服务人员尽快与您取得联系</div>
                <div style="padding: 30px 0; text-align: right;">
                    <a id="submit" style="line-height: 36px; background: #d30000; color: #fff; padding: 0px 50px; display: inline-block" href="javascript:;">确认提交</a>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>

<?php

$js = <<<JS

let total = $total;

$(document).on('change', 'select[ajax-select]', function(){
    var select = $(this);
    var url = $(this).attr('ajax-select');
    var parentId = $(this).val();
    var form = select.closest('form');

    select.nextAll('select').remove();

    $.post(url, {_crsf: form.children('input[name="_csrf"]').val(), parent_id: parentId}, function(datas){
        if(datas.html){
            select.after(datas.html);
        }
    }, 'json');
});

$('#submit').click(function(){
    let form = $(this).closest('form');
    
    if(!form.find('input[name="name"]').val()){
        alert('请填写姓名！');
        return;
    }
    
    if(!form.find('input[name="address"]').val()){
        alert('请填写详细地址！');
        return;
    }
    
    if(total === 0){
        alert('请去购物车选择要提交的商品！');
        return;
    }
    
    form.submit();
});

JS;

$this->registerJs($js);
